<?php

class Kvs_model extends MY_Model {

	private $news_at_home_key = 'NEWSATHOME';
	
	public function __construct() {
		parent::__construct();
		$this->table = 'alsa_kvs';
	}

	public function getOne($key) {
		if (!($row = $this->find(array(array('where' ,'key', $key)))))
			return false;
		
		return $row->value;
	}
	
	public function setOne($key, $value){
		$data = array('value' => $value);
		if($this->getOne($key)){
			$this->db->where('key',$key);
			return $this->db->update($this->table, $data);
		}else{
			$data['key'] = $key;
			return $this->db->insert($this->table, $data);
		}
	}
	
	public function getNewsAtHome(){
		$sIds = $this->getOne($this->news_at_home_key);
		$aIds = json_decode($sIds, true);
		if(is_array($aIds))
			return $aIds;
		return array(1,2,3,4,5,6);
	}
	
	public function setNewsAtHome($index, $id){
		$aIds = $this->getNewsAtHome();
		$aIds[$index] = $id;
		$sIds = json_encode($aIds);
		
		return $this->setOne($this->news_at_home_key, $sIds);
	}

}
